﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ProductManager
{
    public partial class ProductCategories : Form
    {
        private string _productID = "";
        public string ProductID
        {
            get { return _productID; }
            set { _productID = value; }
        }

        List<string[]> _categoryTree = new List<string[]>();

        public ProductCategories()
        {
            InitializeComponent();
            _categoryTree.Add(new string[] { "/", "/" });
        }

        public void RefreshProductCategory()
        {
            DataTable categories = DBAccess.GetProductCategory(_productID);
            if (categories != null)
            {
                lvProductCategory.Items.Clear();
                foreach (DataRow row in categories.Rows)
                {
                    lvProductCategory.Items.Add(new ListViewItem(new string[] {
                        row["Name"].ToString(), row["ID"].ToString() }));
                }
            }
        }

        private DataTable GetChildCategory(string parentID)
        {
            if (parentID == "/")
            {
                return DBAccess.GetRootCategory();
            }
            else
            {
                return DBAccess.GetChildCategory(parentID);
            }
        }

        private void RefreshAllCategory()
        {
            DataTable child;
            string indent = "";

            lvAllCategory.Items.Clear();
            foreach (string[] category in _categoryTree)
            {
                lvAllCategory.Items.Add(new ListViewItem(category));
            }
            if (_categoryTree.Count > 1)
            {
                indent = "|--";
                for (int i = 0; i < _categoryTree.Count - 2; i++)
                {
                    indent = "    " + indent;
                }
            }
            child = GetChildCategory(_categoryTree[_categoryTree.Count - 1][1]);
            foreach (DataRow row in child.Rows)
            {
                lvAllCategory.Items.Add(new ListViewItem(new string[] {
                    indent + row["Name"].ToString(), row["ID"].ToString() }));
            }
        }

        private void ProductCategories_Load(object sender, EventArgs e)
        {
            RefreshAllCategory();
            RefreshProductCategory();
        }

        private void btnAddCategory_Click(object sender, EventArgs e)
        {
            if (lvAllCategory.SelectedIndices.Count == 1)
            {
                DBAccess.AddProductCategory(_productID, lvAllCategory.SelectedItems[0].SubItems[1].Text);
                RefreshProductCategory();
            }
        }

        private void btnDeleteCategory_Click(object sender, EventArgs e)
        {
            if (lvProductCategory.SelectedItems.Count == 1)
            {
                if (DBAccess.DeleteProductCategory(_productID, lvProductCategory.SelectedItems[0].SubItems[1].Text))
                {
                    RefreshProductCategory();
                }
                else
                {
                    MessageBox.Show("Delete category failed!!!");
                }
            }
            else
            {
                MessageBox.Show("Please choose one category!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void lvAllCategory_DoubleClick(object sender, EventArgs e)
        {
            if (lvAllCategory.SelectedIndices.Count == 1)
            {
                if (lvAllCategory.SelectedIndices[0] < _categoryTree.Count)
                {
                    while (_categoryTree.Count - 1 > lvAllCategory.SelectedIndices[0])
                    {
                        _categoryTree.RemoveAt(_categoryTree.Count - 1);
                    }
                }
                else
                {
                    _categoryTree.Add(new string[] {
                        lvAllCategory.SelectedItems[0].SubItems[0].Text,
                        lvAllCategory.SelectedItems[0].SubItems[1].Text });
                }
                RefreshAllCategory();
            }
        }
    }
}
